/**
 * Definition for singly-linked list.
 * **/
  class ListNode {
     int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode cur1 = headA;
        ListNode cur2 = headB;
        int counta = getLen(headA);
        int countb = getLen(headB);
        while(counta > countb && cur1.next != null){
            cur1 = cur1.next;
            counta --;
        }
        while(countb > counta && cur2.next != null){
            cur2 = cur2.next;
            countb --;
        }
        for(int i = 0;i < countb;i ++){
            if(cur1 == cur2){
                return cur1;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }
    public int getLen(ListNode head){
        int count = 1;
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
            count ++;
        }
        return count;
    }
}